﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MaskManager : MonoBehaviour
{
    public static MaskManager Instance
    {
        get
        {
            if (_Instance == null)
            {
                _Instance = FindObjectOfType<MaskManager>();
            }
            if (!_Instance)
            {
                GameObject obj = new GameObject("MaskManager");
                _Instance = obj.AddComponent<MaskManager>();
            }
            return _Instance;
        }
        set
        {
            _Instance = value;
        }
    }
    private static MaskManager _Instance;
    int width;
    int height;
    static float xGap=ConstOfMap.xGap;
    static float yGap=ConstOfMap.yGap;
    static Vector3 originPos = ConstOfMap.originPos;
    Mask[,] masks;
    bool[,] clear;

    public Sprite[] texture=new Sprite[ConstOfMap.MASKMAX];

    int[,] changeMask = new int[3, 3] {{4,5,1 },
                                        { 12,15,3 },
                                        { 8,10,2 } };
    public GameObject prefabMask;

    private void Awake()
    {
        if (ConstOfMap.loadgame)
        {
            width = ConstOfMap.save.width;
            height = ConstOfMap.save.height;
        }
        else
        {
            width = ConstOfMap.width;
            height = ConstOfMap.height;
        }
        masks = new Mask[width + 2, height + 2];
        clear = new bool[width + 2, height + 2];
    }
    // Start is called before the first frame update
    void Start()
    {
        for (int i = 0; i < ConstOfMap.MASKMAX; i++)
        {
            texture[i] = Resources.Load<Sprite>("Image/Mask/" + ((MaskType)i).ToString());
            //Debug.Log(texture[i]);
        }
        //initiateMask();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void initiateMask()
    {
        GameObject obj;
        for (int i = 0; i < width + 2; ++i)
        {
            for (int j = 0; j < height + 2; ++j)
            {
                obj=Instantiate(prefabMask, new Vector3(originPos.x + xGap * (i - 1), originPos.y + yGap * (j - 1), originPos.z),Quaternion.identity);
                masks[i, j] = obj.GetComponent<Mask>();
                masks[i, j].init();
            }
        }
    }

    public void breakStone(Vector2Int LocgicPos)
    {
        for(int i=0;i<3;i++)
            for(int j=0;j<3;j++)
            {
                int mx = LocgicPos.x + i - 1, my = LocgicPos.y + j - 1;
                if (Function.OutOfRange(mx, my))
                    continue;
                Black(mx, my);
                if(masks[mx + 1, my + 1] != null)
                {
                    masks[mx + 1, my + 1].change(changeMask[i, j]);
                    if (masks[mx + 1, my + 1].type == MaskType.EMPTY)
                    {
                        Destroy(masks[mx + 1, my + 1].gameObject);
                        masks[mx + 1, my + 1] = null;
                        clear[mx + 1, my + 1] = true;
                    }
                }
            }
    }

    public void Black(int x, int y)
    {
        x = x + 1;
        y = y + 1;
        if (x < 0 || y < 0 || x > width + 1 || y > height + 1)
            return;
        if(masks[x, y] == null && !clear[x, y])
        {
            masks[x, y] = Instantiate(prefabMask, new Vector3(originPos.x + xGap * (x - 1), originPos.y + yGap * (y - 1), originPos.z), Quaternion.identity).GetComponent<Mask>();
            masks[x, y].init();
        }
    }
}
